﻿using System;
using System.Windows.Forms;
using OFX.Aggregator.Infrastructure;

namespace OFX.Aggregator.Implementation.ViaVerde
{
	public partial class PasteXmlStatementUserControl : UserControl
	{
		private readonly IStatementAction statementAction;
		private readonly StatementSource statementSource;
		private readonly IStatementProcessingWorkflow workflow;

		public PasteXmlStatementUserControl(StatementSource source, IStatementAction action,
		                                    IStatementProcessingWorkflow workflow)
		{
			statementSource = source;
			statementAction = action;
			this.workflow = workflow;

			InitializeComponent();
		}

		private void UpdateFormState()
		{
			buttonPaste.Enabled = statementSource.Retrieval.CanRetrieveStatement();
		}

		private void buttonPaste_Click(object sender, EventArgs e)
		{
			workflow.PerformWorkflow(statementSource, statementAction);
		}

		private void PasteXmlStatementUserControl_ParentChanged(object sender, EventArgs e)
		{
			UpdateFormState();

			if (ParentForm != null)
			{
				ParentForm.Activated += ParentForm_Activated;
			}
		}

		private void ParentForm_Activated(object sender, EventArgs e)
		{
			UpdateFormState();
		}
	}
}